// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraTeamStatics.h"

#include "Engine/Engine.h"
#include "Engine/World.h"
#include "LyraLogChannels.h"
#include "Teams/LyraTeamDisplayAsset.h"
#include "Teams/LyraTeamSubsystem.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraTeamStatics)

class UTexture;

//////////////////////////////////////////////////////////////////////

/**
 * 从对象查找团队
 */
void ULyraTeamStatics::FindTeamFromObject(const UObject* Agent, bool& bIsPartOfTeam, int32& TeamId, ULyraTeamDisplayAsset*& DisplayAsset, bool bLogIfNotSet)
{
	bIsPartOfTeam = false; // 初始化不属于团队
	TeamId = INDEX_NONE; // 初始化团队ID为无
	DisplayAsset = nullptr; // 初始化显示资源为空

	if (UWorld* World = GEngine->GetWorldFromContextObject(Agent, EGetWorldErrorMode::LogAndReturnNull)) // 获取世界上下文
	{
		if (ULyraTeamSubsystem* TeamSubsystem = World->GetSubsystem<ULyraTeamSubsystem>()) // 获取团队子系统
		{
			TeamId = TeamSubsystem->FindTeamFromObject(Agent); // 从对象查找团队ID
			if (TeamId != INDEX_NONE) // 如果团队ID有效
			{
				bIsPartOfTeam = true; // 设置属于团队

				DisplayAsset = TeamSubsystem->GetTeamDisplayAsset(TeamId, INDEX_NONE); // 获取团队显示资源

				if ((DisplayAsset == nullptr) && bLogIfNotSet) // 如果显示资源为空且需要记录日志
				{
					UE_LOG(LogLyraTeams, Log, TEXT("FindTeamFromObject(%s) called too early (found team %d but no display asset set yet"), *GetPathNameSafe(Agent), TeamId); // 记录日志
				}
			}
		}
		else
		{
			UE_LOG(LogLyraTeams, Error, TEXT("FindTeamFromObject(%s) failed: Team subsystem does not exist yet"), *GetPathNameSafe(Agent)); // 记录错误日志
		}
	}
}

/**
 * 获取团队显示资源
 */
ULyraTeamDisplayAsset* ULyraTeamStatics::GetTeamDisplayAsset(const UObject* WorldContextObject, int32 TeamId)
{
	ULyraTeamDisplayAsset* Result = nullptr; // 初始化结果为空
	if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull)) // 获取世界上下文
	{
		if (ULyraTeamSubsystem* TeamSubsystem = World->GetSubsystem<ULyraTeamSubsystem>()) // 获取团队子系统
		{
			return TeamSubsystem->GetTeamDisplayAsset(TeamId, INDEX_NONE); // 返回团队显示资源
		}
	}
	return Result; // 返回结果
}

/**
 * 获取团队标量值（有回退）
 */
float ULyraTeamStatics::GetTeamScalarWithFallback(ULyraTeamDisplayAsset* DisplayAsset, FName ParameterName, float DefaultValue)
{
	if (DisplayAsset) // 如果显示资源有效
	{
		if (float* pValue = DisplayAsset->ScalarParameters.Find(ParameterName)) // 查找标量参数
		{
			return *pValue; // 返回参数值
		}
	}
	return DefaultValue; // 返回默认值
}

/**
 * 获取团队颜色（有回退）
 */
FLinearColor ULyraTeamStatics::GetTeamColorWithFallback(ULyraTeamDisplayAsset* DisplayAsset, FName ParameterName, FLinearColor DefaultValue)
{
	if (DisplayAsset) // 如果显示资源有效
	{
		if (FLinearColor* pColor = DisplayAsset->ColorParameters.Find(ParameterName)) // 查找颜色参数
		{
			return *pColor; // 返回颜色值
		}
	}
	return DefaultValue; // 返回默认值
}

/**
 * 获取团队纹理（有回退）
 */
UTexture* ULyraTeamStatics::GetTeamTextureWithFallback(ULyraTeamDisplayAsset* DisplayAsset, FName ParameterName, UTexture* DefaultValue)
{
	if (DisplayAsset) // 如果显示资源有效
	{
		if (TObjectPtr<UTexture>* pTexture = DisplayAsset->TextureParameters.Find(ParameterName)) // 查找纹理参数
		{
			return *pTexture; // 返回纹理
		}
	}
	return DefaultValue; // 返回默认值
}